package com.lanlian.cayman.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.lanlian.cayman.utils.MResUtil;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.cayman.LLApplication;
import com.lanlian.cayman.R;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/23 11:37
 *     desc   : 资金账户页面
 *     version: 1.0
 * </pre>
 */
public class FundAccountActivity extends BaseActivity {

    private TextView mTvPositionPnl;
    private TextView mTvAvailableFunds;
    private TextView mTvActualPnl;
    private TextView mTvDeposit;
    private TextView mTvOvernigthtDeposit;
    private TextView mTvInMoney;
    private TextView mTvOutMoney;
    private TextView mTvDynamicProfit;
    private TextView mTvYesterdaySettlement;
    private TextView mTvRiskRate;
    private TextView mTvDepositType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fund_account, true);
        initView();
        setData();
        if (mDBManager.getUser() != null && mDBManager.getUser().getAccountType() == 1) {
            mTvDepositType.setText("维持保证金");
            mTvOvernigthtDeposit.setText(String.format("%.2f", LLApplication.getUnitAssetInfo().getKeepBzj()));
        } else {
            mTvDepositType.setText("隔夜保证金");
            mTvOvernigthtDeposit.setText(String.format("%.2f", LLApplication.getOvernightDeposit()));
        }
        mTvDeposit.setText(String.format("%.2f", LLApplication.getDeposit()));
        if (LLApplication.getActualPnl() > 0) {
            mTvActualPnl.setTextColor(MResUtil.getUpColor());
            mTvActualPnl.setText(String.format("+ %.2f", LLApplication.getActualPnl()));
        } else if (LLApplication.getActualPnl() < 0) {
            mTvActualPnl.setTextColor(MResUtil.getDownColor());
            mTvActualPnl.setText(String.format("%.2f", LLApplication.getActualPnl()));
        } else {
            mTvActualPnl.setTextColor(Color.parseColor("#FFFFFF"));
            mTvActualPnl.setText(String.format("%.2f", LLApplication.getActualPnl()));
        }
        mTvInMoney.setText(String.format("%.2f", LLApplication.getIntoCash()));
        mTvOutMoney.setText(String.format("%.2f", LLApplication.getWithdrawCash()));
        mTvYesterdaySettlement.setText(String.format("%.2f", LLApplication.getBeginCash()));
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("资金账户");
        mTvPositionPnl = findViewById(R.id.tv_position_pnl);
        mTvAvailableFunds = findViewById(R.id.tv_available_funds);
        mTvActualPnl = findViewById(R.id.tv_actual_pnl);
        mTvDeposit = findViewById(R.id.tv_deposit);
        mTvOvernigthtDeposit = findViewById(R.id.tv_overnigtht_deposit);
        mTvInMoney = findViewById(R.id.tv_in_money);
        mTvOutMoney = findViewById(R.id.tv_out_money);
        mTvDynamicProfit = findViewById(R.id.tv_dynamic_profit);
        mTvYesterdaySettlement = findViewById(R.id.tv_yesterday_settlement);
        mTvRiskRate = findViewById(R.id.tv_risk_rate);
        mTvDepositType = findViewById(R.id.tv_deposit_type);
    }

    private void setData() {
        mTvAvailableFunds.setText(String.format("$ %.2f", LLApplication.getAvailableFunds()));
        if (LLApplication.getPositionPnl() > 0) {
            mTvPositionPnl.setTextColor(MResUtil.getUpColor());
            mTvPositionPnl.setText(String.format("+ %.2f", LLApplication.getPositionPnl()));
        } else if (LLApplication.getPositionPnl() < 0) {
            mTvPositionPnl.setTextColor(MResUtil.getDownColor());
            mTvPositionPnl.setText(String.format("%.2f", LLApplication.getPositionPnl()));
        } else {
            mTvPositionPnl.setTextColor(Color.parseColor("#FFFFFF"));
            mTvPositionPnl.setText(String.format("%.2f", LLApplication.getPositionPnl()));
        }
        mTvDynamicProfit.setText(String.format("%.2f", LLApplication.getDynamicPnl()));
        if (LLApplication.getDynamicPnl() == 0) {
            mTvRiskRate.setText("0.00%");
        } else {
            double risk = LLApplication.getDeposit2() / LLApplication.getDynamicPnl();
            mTvRiskRate.setText(String.format("%.2f%%", risk * 100));
        }
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        switch (event.getCode()) {
            case EventCode.EVENT_MARKET:
                setData();
                break;
            default:
                break;
        }
    }
}
